//
//  MainTabbarController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/31.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "MainTabbarController.h"
#import "UINavigationItem+Zy.h"
#import "Config.h"

@interface MainTabbarController ()
{
    NSInteger _index;
}
@end

@implementation MainTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self setTabBarAttr];
    for (UIViewController *viewController in self.childViewControllers)
        viewController.edgesForExtendedLayout = UIRectEdgeNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMessage:) name:XHNewMessage object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(touchNotification:) name:XHMessage object:nil];
    NSInteger index = [UserDefaultsTool integerForKey:XHMessage];
    [self tabBar:self.tabBar didSelectItem:self.tabBar.items[index]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    self.selectedIndex = item.tag;
    UIViewController *newVC = self.childViewControllers[item.tag];
    [self.navigationItem copyFromeItem:newVC.navigationItem];
}

- (void)animationWithIndex:(NSInteger)index {
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews)
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")])
            [tabbarbuttonArray addObject:tabBarButton];
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount = 1;
    pulse.autoreverses = YES;
    pulse.fromValue = @(0.7);
    pulse.toValue = @(1.3);
    [[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];
    _index = index;
}

- (void)setTabBarAttr {
    self.tabBar.translucent = NO;
    self.tabBar.backgroundColor = RGBColor(237, 238, 239);
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName : XHConfigColor} forState:UIControlStateSelected];
    
    for (UITabBarItem *item in self.tabBar.items) {
        item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.selectedImage = [item.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}

- (void)newMessage:(NSNotification *)notification {
//    XHLog(@"notification: %@", notification)
    NSInteger count = [UserDefaultsTool integerForKey:XHNewMessage];
    NSString *badgeValue = nil;
    if (count)
        badgeValue = [NSString stringWithFormat:@"%ld", count];
    UITabBarItem *item = self.tabBar.items[1];
    item.badgeValue = [badgeValue isEqualToString:@"0"] ? nil : badgeValue;
}

- (void)touchNotification:(NSNotification *)notification {
    UITabBarItem *item = self.tabBar.items[1];
    [self tabBar:self.tabBar didSelectItem:item];
}

@end
